package com.senney.file.io;

import org.junit.Test;

import java.io.*;

/**
 * @author Lanmengye@outlook.com
 * @date 2021/1/2 18:37
 */
public class IOStreamDemo {
    @Test
    public void readByByte() {
        File f = new File("C:\\Users\\Lanme\\Desktop\\zookeeper.txt");
        InputStream is = null;
        try {
            is = new FileInputStream(f);
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = is.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void writeByByte() {
        File f = new File("F:\\test\\a.txt");
        OutputStream os = null;
        try {
            os = new FileOutputStream(f, true);
            os.write("Hello, FileOutputStream\n".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void readByChar() {
        File f = new File("C:\\Users\\Lanme\\Desktop\\zookeeper.txt");
        Reader reader = null;
        try {
            reader = new FileReader(f);
            char[] chars = new char[1024];
            int len = 0;
            while ((len = reader.read(chars)) != -1) {
                System.out.println(new String(chars, 0, len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void writeByChar() {
        File f = new File("F:\\test\\a.txt");
        Writer writer = null;
        try {
            writer = new FileWriter(f, true);
            writer.write("Hello, FileWriter\n".toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void copyFile() {
        long start = System.currentTimeMillis();
        File src = new File("D:\\CourseVideo\\Database\\csdn-andy-mysql\\第四节课.mp4");
        File dest = new File("F:\\test\\a.mp4");
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new BufferedInputStream(new FileInputStream(src));
            os = new BufferedOutputStream(new FileOutputStream(dest));
            byte[] bytes = new byte[32 * 1024];
            int len = 0;
            while ((len = is.read(bytes)) != -1) {
                os.write(bytes, 0, len);
            }
            long end = System.currentTimeMillis();
            System.out.println((end - start) / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                os.flush();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
